package com.cq.school.service;

import com.cq.school.entity.Pictures;
import com.cq.school.entity.School;
import com.cq.school.entity.Specialty;
import com.cq.school.repository.PicturesDao;
import com.cq.school.repository.SchoolDao;
import com.cq.school.repository.SpecialtyDao;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: huke
 * Date: 13-12-4
 * Time: 下午9:40
 * To change this template use File | Settings | File Templates.
 */
@Component
@Transactional
public class SchoolService {
    @Autowired
    private SchoolDao schoolDao;
    @Autowired
    private PicturesDao picturesDao;

    @Autowired
    private SpecialtyDao specialtyDao;

    public School saveSchool(School school){
        if(school.getId()==null)
            school.setCreateTime(new Date());
        school.setUpdateTime(new Date());
        return schoolDao.save(school);
    }
    public Page<School> listSchool(Map<String, Object> searchParams, int pageNumber, int pageSize){
        PageRequest pageRequest = buildPageRequest(pageNumber, pageSize);
        Specification<School> spec = buildSpecification(searchParams);
        return schoolDao.findAll(spec, pageRequest);
    }

    public List<School> listSchool()
    {
        return IteratorUtils.toList(schoolDao.findAll().iterator());
    }

    public void updateRecommend(long schoolId,int recommend,int type)
    {
        if(type==0)
            schoolDao.recommendSchool(schoolId,recommend);
        else
            schoolDao.cancelRecommen(schoolId,recommend);
    }
    /**
     * 创建分页请求.
     */
    private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
        return new PageRequest(pageNumber - 1, pagzSize, new Sort(Sort.Direction.DESC, "id"));
    }
    /**
     * 创建动态查询条件组合.
     */
    private Specification<School> buildSpecification(Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<School> spec = DynamicSpecifications.bySearchFilter(filters.values(), School.class);
        return spec;
    }
    public School getSchool(Long id){
        return schoolDao.findOne(id);
    }

     public void deleteSchool(Long id){
         schoolDao.delete(id);
     }

    public List<School> listRecommenSchool(int recommon,int size){
        return schoolDao.listRecommenSchool(recommon,size);
    }

    public List<Specialty> findSpecialtyBySchoolId(long schooolId){
        return specialtyDao.findBySchoolId(schooolId);
    }

    public Specialty saveSpecialty(Specialty specialty){
        if(specialty.getId()==null)
            specialty.setCreateTime(new Date());
        return specialtyDao.save(specialty);
    }

    public void deleteSpecialty(long id){
        specialtyDao.delete(id);
    }
    public void saveSchoolPicture(Pictures pictures){
        if(pictures.getId()==null)
            pictures.setCreateTime(new Date());
        picturesDao.save(pictures);
    }
    public Map<Long,String> getSpecialtiyMap(){
        Map<Long,String> map = new HashMap<Long, String>();
        for(Iterator<Specialty> it=specialtyDao.findAll().iterator();it.hasNext();){
            Specialty specialty = it.next();
            map.put(specialty.getId(),specialty.getName());
        }

        return map;
    }

    public List<Pictures> listPictures(long schoolId){
        return picturesDao.findBySchoolId(schoolId);
    }

    public void deleteSchoolPicture(long id){
        picturesDao.delete(id);
    }
}
